package com.molyfun.parents.activity.classroom;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.hyphenate.easeui.model.styles.EaseMessageListItemStyle;
import com.molyfun.parents.R;
import com.molyfun.parents.im.ChatMessageList;
import com.molyfun.parents.im.adapter.MessageAdapter;
import com.molyfun.parents.im.chatrow.BaseChatRow;
import com.molyfun.parents.im.model.ChatMessageEntity;

public class ClassRoomMessageList extends RelativeLayout implements View.OnClickListener{

    protected static final String TAG = "EaseChatMessageList";
    protected ListView listView;
    protected SwipeRefreshLayout swipeRefreshLayout;
    protected Context context;
//    protected EMConversation conversation;
    protected int chatType;
    protected String mGroupId;
    protected MessageAdapter messageAdapter;
    protected BaseChatRow.MsgResendCallback mMsgResendCallback;

    protected EaseMessageListItemStyle itemStyle;


    public ClassRoomMessageList(Context context, AttributeSet attrs, int defStyle) {
        this(context, attrs);
    }

    public ClassRoomMessageList(Context context, AttributeSet attrs) {
        super(context, attrs);
        parseStyle(context, attrs);
        init(context);
    }

    public ClassRoomMessageList(Context context) {
        super(context);
        init(context);
    }

    private void init(Context context) {
        this.context = context;
        LayoutInflater.from(context).inflate(R.layout.ease_chat_message_list, this);
        swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.chat_swipe_layout);
        listView = (ListView) findViewById(R.id.list);
    }

    /**
     * init widget
     *
     * @param groupId
     * @param chatType
     */
    public void init(String groupId, int chatType) {
        init(groupId, chatType, null);
    }

    public void init(String groupId, int chatType, BaseChatRow.MsgResendCallback msgResendCallback) {
        this.chatType = chatType;
        this.mGroupId = groupId;
        this.mMsgResendCallback = msgResendCallback;

//        conversation = EMClient.getInstance().chatManager().getConversation(groupId, EaseCommonUtils.getConversationType(chatType), true);
        messageAdapter = new MessageAdapter(context, listView, true, msgResendCallback);
        messageAdapter.setItemStyle(itemStyle);
        // set message adapter
        listView.setAdapter(messageAdapter);

        refreshSelectLast();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){

        }
    }

    protected void parseStyle(Context context, AttributeSet attrs) {
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.EaseChatMessageList);
        EaseMessageListItemStyle.Builder builder = new EaseMessageListItemStyle.Builder();
        builder.showAvatar(ta.getBoolean(R.styleable.EaseChatMessageList_msgListShowUserAvatar, true))
                .showUserNick(ta.getBoolean(R.styleable.EaseChatMessageList_msgListShowUserNick, false))
                .myBubbleBg(ta.getDrawable(R.styleable.EaseChatMessageList_msgListMyBubbleBackground))
                .otherBuddleBg(ta.getDrawable(R.styleable.EaseChatMessageList_msgListMyBubbleBackground));

        itemStyle = builder.build();
        ta.recycle();
    }

    public void addData(ChatMessageEntity var) {
        messageAdapter.addData(var);
    }

    public void clearData(){
        messageAdapter.clearData();
    }

    public void addDataListHeader(java.util.List<ChatMessageEntity> var) {
        if (messageAdapter != null) {
            messageAdapter.addLocallDataListHeader(var);
        }
    }

    /**
     * refresh
     */
    public void refresh() {
        if (messageAdapter != null) {
            messageAdapter.refresh();
        }
    }

    /**
     * refresh and jump to the last
     */
    public void refreshSelectLast() {
        if (messageAdapter != null) {
            messageAdapter.refreshSelectLast();
        }
    }

    /**
     * refresh and jump to the position
     *
     * @param position
     */
    public void refreshSeekTo(int position) {
        if (messageAdapter != null) {
            messageAdapter.refreshSeekTo(position);
        }
    }

    public ListView getListView() {
        return listView;
    }

    public SwipeRefreshLayout getSwipeRefreshLayout() {
        return swipeRefreshLayout;
    }

    public ChatMessageEntity getItem(int position) {
        return messageAdapter.getItem(position);
    }

    public void setShowUserNick(boolean showUserNick) {
        itemStyle.setShowUserNick(showUserNick);
    }

    public boolean isShowUserNick() {
        return itemStyle.isShowUserNick();
    }

    /**
     * set click listener
     *
     * @param listener
     */
    public void setItemClickListener(ChatMessageList.MessageListItemListener listener) {
        if (messageAdapter != null) {
            messageAdapter.setItemClickListener(listener);
        }
    }

}
